package com.news.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.bean.Add_admin;
import com.news.dao.Add_adminDao;
import com.news.dao.impl.Add_adminDaoImpl;

public class Add_adminServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Map map = new HashMap();
        Add_admin ad = new Add_admin();
        Add_adminDao ado = new Add_adminDaoImpl();
        String admin = req.getParameter("admin");
        String id = req.getParameter("id");
        String account_no = req.getParameter("account_no");
        String password = req.getParameter("password");
//		System.out.println("admin"+"="+admin);
        //	System.out.println("id"+"="+admin);
        System.out.println("account_no" + "=" + account_no);
        System.out.println("password" + "=" + password);
        if (id != null) {
            ad.setId(Integer.valueOf(id));
        }
        ad.setAccount_no(account_no);
        ad.setPassword(password);
        if (admin != null) {
            if (admin.equals("showuser")) {
                List<Add_admin> list = ado.showuser(map);
                req.setAttribute("list", list);
                //System.out.println(list.size());
                req.getRequestDispatcher("adminList.jsp").forward(req, resp);
            }
            if (admin.equals("login")) {
                PrintWriter writer = resp.getWriter();
                if (ado.login(account_no, password) == true) {
                    System.out.println("+++++++++++++++++++++++");
                    req.getSession().setAttribute("login", true);
                    writer.print(true);
                } else {
                   writer.print(false);
                }
                writer.flush();
                writer.close();
            }
            if (admin.equals("adduser")) {
                ado.adduser(ad);
//					System.out.println("admin.equals('adduser')"+"=="+admin.equals("adduser"));
//					System.out.println("ado.adduser(ad)"+"=="+ado.adduser(ad));
                req.getRequestDispatcher("admin?admin=showuser").forward(req, resp);
            }
            if (admin.equals("deluser")) {
                ado.deluser(Integer.valueOf(id));
                req.getRequestDispatcher("admin?admin=showuser").forward(req, resp);
            }
            if (admin.equals("edituser")) {
                ado.edituser(ad);
//					System.out.println("admin.equals('edituser')"+"=="+admin.equals("edituser"));
//					System.out.println("ado.edituser()"+"=="+ado.edituser(ad));
                req.getRequestDispatcher("admin?admin=showuser").forward(req, resp);
            }
        }

    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
